#include "admin.h"

int acceptfd;
char name[N]="",pass[N]="";
char sql[N]="";
char  **result;
int  nrow,ncolumn;
char key;//判定登陆用户权限
emp_inf inf;
int admin(int fd)
{
	acceptfd = fd;
	ret = option_choose();
	return ret;
}

int option_choose()
{
	int bit;
	memset(buf,0,sizeof(buf));
	bit = recv(acceptfd,buf,sizeof(buf),0);//接收判断选项
	switch(buf[0])
	{
	case '1':
			printf("有管理员正在登陆\n");
			key = '0';
			bit = admin_login();
			break;
	case '2':
			printf("有员工正在登陆\n");
			key = '1';
			bit = employ_login();
			break;
	}
	return bit;
}

int admin_login()
{
int bit;
char key = '0';
recv(acceptfd,name,sizeof(name),0);
recv(acceptfd,pass,sizeof(pass),0);
printf("acc = %s 	pass = %s\n",name,pass);
sprintf(sql,"select * from account where key = \"%c\" and account =  \"%s\" and  password = \"%s\" ;",\
key,name,pass);
ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
if(nrow <= 0)
{	
	strcpy(buf,"10");
	send(acceptfd,buf,sizeof(buf),0);
	printf("select error\n");
	memset(buf,0,sizeof(buf));
	return 10;//select error
}
else{
	strcpy(buf,"11");//13 represent ok
	send(acceptfd,buf,sizeof(buf),0);//确认信号
		}
fprintf(stdout,"管理员账户：%s\n",name);
while(1){
	memset(buf,0,sizeof(buf));	
	recv(acceptfd,buf,sizeof(buf),0);
	switch(buf[0]){
	case '1':
			printf("管理员正在执行员工信息添加操作\n");
			bit = admin_opt1(1);
			break;
	case '2':
			printf("管理员正在执行员工信息修改操作\n");
			bit =  admin_opt1(2);
			break;
	case '3':
			printf("管理员正在执行员工信息删除操作\n");
			bit = admin_opt2(3);
			break;
	case '4':
			printf("管理员正在执行员工信息查找操作\n");
			bit = admin_opt2(4);
			break;
	case '5':
			printf("管理员正在执行查看员工查询记录操作\n");
			bit = admin_opt2(5);
			break;
	case '6':
			bit = 1;
			return bit;
						}
				}
}

int employ_login()
{
int bit;
char key = '1';
recv(acceptfd,name,sizeof(name),0);
recv(acceptfd,pass,sizeof(pass),0);
printf("acc=%s 	pass = %s\n",name,pass);
sprintf(sql,"select * from account where key = \"%c\" and account =  \"%s\" and  password = \"%s\" ;",\
key,name,pass);
ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
if(nrow <= 0)
{	
	strcpy(buf,"12");
	send(acceptfd,buf,sizeof(buf),0);
	printf("select error\n");
	return 12;//select error
}
else{
	strcpy(buf,"13");//13 represent ok
	send(acceptfd,buf,sizeof(buf),0);
}
fprintf(stdout,"员工账户：%s\n",name);
while(1){
	memset(buf,0,sizeof(buf));	
	recv(acceptfd,buf,sizeof(buf),0);
	switch(buf[0]){
	case '1':
			printf("员工正在执行信息查找操作\n");
			employee_opt();
			memset(buf,0,sizeof(buf));
			break;
	case '2':
			bit = 1;
			memset(buf,0,sizeof(buf));
			return bit;
						}
				}
}
int employee_opt()
{
		char key = '1';
		char s_time[128];
		int i,ret,num=0;
		time_t t;
		struct tm *TM = NULL;
		t = time(&t);
		TM = localtime(&t);
		sprintf(s_time,"%d-%d-%d,%d:%d:%d\n",TM->tm_year+1900,\
TM->tm_mon+1,TM->tm_mday,TM->tm_hour,TM->tm_min,TM->tm_sec);
		recv(acceptfd,&inf,sizeof(inf),0);//接收结构体
		if(strncmp(name,inf.name,strlen(name))!=0){
			strcpy(buf,"26");
			send(acceptfd,buf,sizeof(buf),0);
			printf("name != inf.name\n");
			return 26;
		}
		sprintf(sql,"insert into history values(\"%c\",\"%s\",\"%s\");",key,inf.name,s_time);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			printf("history insert err:%d\n",ret);
			return 1;
		}
		else 
		{
			printf("history insert succeed\n");
		}
		printf("查询员工姓名为：%s       年龄为 %s\n",inf.name,inf.age);
		sprintf(sql,"select * from employee where name = \"%s\" and age = \"%s\";",inf.name,inf.age);
		ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
		if(nrow <= 0)
		{
			strcpy(buf,"24");
			send(acceptfd,buf,sizeof(buf),0);
			printf("select error\n");
			return 24;
		}
		strcpy(buf,"25");
		 send(acceptfd,buf,sizeof(buf),0);//发送运行结果代码
		send(acceptfd,&nrow,sizeof(nrow),0);//发送数据行数(不包括列名那一行，但发送数据时会包含列名)
		while(0<=nrow)
		{
				for(i=0;i<ncolumn;i++)
       		 {
            	ret = send(acceptfd,result[num],1024,0);
				printf("%-7s",result[num]);
				num++;
       		 }
			putchar(10);
        	nrow--;
		}
		return 25;
}
int admin_opt1(int opt_num)
{
	char key = '1';
	if(opt_num == 1)
	{
		recv(acceptfd,&inf,sizeof(inf),0);
		sprintf(sql,"insert into employee values(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\");",\
		inf.name,inf.age,inf.tel,inf.addr,inf.job,inf.date,inf.salary,inf.grade);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			strcpy(buf,"14_1");
			send(acceptfd,buf,sizeof(buf),0);
			printf("insert err:%d\n",ret);
			return 14;
		}
		else 
		{
			strcpy(buf,"14_2");
			send(acceptfd,buf,sizeof(buf),0);
			printf("insert succeed\n");
		}
		sprintf(sql,"insert into account values(\"%c\",\"%s\",\"%s\");",key,inf.account,inf.pass);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			strcpy(buf,"15_1");
			send(acceptfd,buf,sizeof(buf),0);
			printf("insert account err:%d\n",ret);
			return 15;
		}
		else 
		{
			strcpy(buf,"15_2");
			send(acceptfd,buf,sizeof(buf),0);
			printf("insert passord succeed\n");
		}
		return 15;
	}
	else if(opt_num == 2)
	{
	recv(acceptfd,&inf,sizeof(inf),0);
	sprintf(sql,"delete from employee where name = \"%s\" and age = \"%s\";",inf.name,inf.age);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			printf("修改前删除失败\n");
		}
		sprintf(sql,"insert into employee values(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\");",\
		inf.name,inf.age,inf.tel,inf.addr,inf.job,inf.date,inf.salary,inf.grade);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			strcpy(buf,"16");
			send(acceptfd,buf,sizeof(buf),0);
			printf("insert err\n");
			return 16;
		}
		else 
		{
			strcpy(buf,"17");
			send(acceptfd,buf,sizeof(buf),0);
			printf("update succeed\n");
			return 17;
		}

	}
}
int admin_opt2(int opt_num)
{
	int i,num=0;
	if(opt_num == 3)
	{
		recv(acceptfd,&inf,sizeof(inf),0);
		printf("删除员工姓名为：%s        年龄为%s\n",inf.name,inf.age);
		sprintf(sql,"delete from account where name = \"%s\";",inf.name);
		sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		sprintf(sql,"delete from employee where name = \"%s\" and age = \"%s\";",inf.name,inf.age);
		ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
		if(ret!=0)
		{
			strcpy(buf,"18");
			send(acceptfd,buf,sizeof(buf),0);
			printf("delete failed\n");
			return 18;
		}
		else
		{
			strcpy(buf,"19");
			send(acceptfd,buf,sizeof(buf),0);
			printf("delete succeed\n");
			return 19;
		}
	}
	else if(opt_num == 4)
	{
		recv(acceptfd,&inf,sizeof(inf),0);
		printf("查询员工姓名为：%s       年龄为 %s\n",inf.name,inf.age);
		sprintf(sql,"select * from employee where name = \"%s\" and age = \"%s\";",inf.name,inf.age);
		ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
		if(nrow <= 0)
		{
			strcpy(buf,"20");
			send(acceptfd,buf,sizeof(buf),0);
			printf("select error\n");
			return 20;
		}
		strcpy(buf,"21");
		 send(acceptfd,buf,sizeof(buf),0);//发送运行结果代码
		send(acceptfd,&nrow,sizeof(nrow),0);//发送数据行数(不包括列名那一行，但发送数据时会包含列名)
		while(0<=nrow)
		{
				for(i=0;i<ncolumn;i++)
       		 {
            	send(acceptfd,result[num],1024,0);
				printf("%-10s",result[num]);
				num++;
       		 }
			putchar(10);
        	nrow--;
		}
	}
	else if(opt_num == 5)
	{
		recv(acceptfd,&inf,sizeof(inf),0);
		printf("查询员工姓名为：%s \n",inf.name);
		sprintf(sql,"select * from history where account = \"%s\";",inf.name);
		ret = sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&errmsg);
		if(nrow <= 0)
		{
			strcpy(buf,"22");
			send(acceptfd,buf,sizeof(buf),0);
			printf("select history error:%s\n",errmsg);
			return 22;
		}
		strcpy(buf,"23");
		 send(acceptfd,buf,sizeof(buf),0);//发送运行结果代码
		send(acceptfd,&nrow,sizeof(nrow),0);//发送数据行数(不包括列名那一行，但发送数据时会包含列名)
		while(0<=nrow)
		{
				for(i=1;i<ncolumn;i++)
       		 {
            	send(acceptfd,result[++num],1024,0);
				printf("%-10s",result[num]);
       		 }
			num++;
			putchar(10);
        	nrow--;
		}
	}
}